<?php

require_once 'inc/common.php';

$stmt = $db->prepare('SELECT fbid, content FROM posts WHERE user_fbid=?');
$stmt->execute(array($_SESSION['me']['fbid']));
$resultset = $stmt->fetchAll();
$postcount = count($resultset);
$matchedposts = array();
if($resultset){
    switch($_GET['category']){
        case "Shouting" :
            $capsCount = array();
            $posts = array();
            $upperCasePattern = "/[A-Z]/";
            foreach($resultset as $s)
            {
                $capsCount[] = preg_match_all($upperCasePattern,$s['content'],$matches);
                $posts[] = $s['content'];
            }
            array_multisort($capsCount, SORT_DESC, $posts);
            $matchedposts = array_slice($posts, 0, 10);
            break;
			
			case "Grammar Nazi" :
				$grammarNaziCount = array();
				$posts = array();
				$grammarnazipattern = '/[:;()\'\"\.\-\!\^\$\+\*\|]/';
				foreach($resultset as $gn)
					{
						$dotsCount[]=preg_match_all($grammarnazipattern,$gn['content'],$matches);
						$posts[] =$gn['content'];
					}
					array_multisort($dotsCount,SORT_DESC,$posts);
					$matchedposts = array_slice($posts,0,10);
    }
	
}

header('Content-Type: application/json');
echo json_encode($matchedposts);
?>